{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用需要的模块\n",
    "import requests\n",
    "import time\n",
    "import json\n",
    "import hashlib\n",
    "import base64"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"code\":\"10105\",\"data\":0,\"desc\":\"illegal access|illegal X-CheckSum\",\"sid\":\"wsr00038f7e@dx121612ef6bad6f2b00\"}\n"
     ]
    }
   ],
   "source": [
    "def main():\n",
    "    # 应用ID (必须为webapi类型应用，并开通人脸比对服务，参考帖子如何创建一个webapi应用：http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=36481)\n",
    "    x_appid = '5f940696'\n",
    "    # 接口密钥(webapi类型应用开通人脸比对服务后，控制台--我的应用---人脸比对---相应服务的apikey)\n",
    "    api_key = '5b5f3549e6a133d59a140054321a808d'\n",
    "    # webapi接口地址\n",
    "    url = 'http://api.xfyun.cn/v1/service/v1/image_identify/face_verification'\n",
    "    # 组装http请求头\n",
    "    x_time = str(int(time.time()))\n",
    "    param = {'auto_rotate': True}\n",
    "    param = json.dumps(param)\n",
    "    x_param = base64.b64encode(param.encode('utf-8'))\n",
    "    m2 = hashlib.md5()\n",
    "    m2.update(str(api_key + x_time + str(x_param, 'utf-8')).encode('utf-8'))\n",
    "    x_checksum = m2.hexdigest()\n",
    "    x_header = {\n",
    "        'X-Appid': x_appid,\n",
    "        'X-CurTime': x_time,\n",
    "        'X-CheckSum': x_checksum,\n",
    "        'X-Param': x_param,\n",
    "    }\n",
    "    # 对图片一和图片二base64编码\n",
    "    with open(r'C:/Users/32950/Pictures/Saved Pictures/hnh.png', 'rb') as f:\n",
    "        f1 = f.read()\n",
    "    with open(r'C:/Users/32950/Pictures/Saved Pictures/wl.png', 'rb') as f:\n",
    "        f2 = f.read()\n",
    "    f1_base64 = str(base64.b64encode(f1), 'utf-8')\n",
    "    f2_base64 = str(base64.b64encode(f2), 'utf-8')\n",
    "    data = {\n",
    "        'first_image': f1_base64,\n",
    "        'second_image': f2_base64,\n",
    "    }\n",
    "    req = requests.post(url, data=data, headers=x_header)\n",
    "    result = str(req.content, 'utf-8')\n",
    "    print(result)  # 错误码链接：https://www.xfyun.cn/document/error-code (code返回错误码时必看)\n",
    "    return\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
